<template>
    <div>
        <p>
            <button v-on:click="list()" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh"></i>
                刷新
            </button>
        </p>

        <div class="row">
            <div class="col-md-6">
                <textarea id="resource-textarea" class="form-control" v-model="resourceStr" name="resource" rows="10"></textarea>

                <br>
                <button id="save-btn" type="button" class="btn btn-primary" v-on:click="save()">
                    保存
                </button>
            </div>
            <div class="col-md-6">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>

    </div>
</template>


<script scope>
    import axios from 'axios';
    import {defineComponent, onMounted, ref} from 'vue';

    export default defineComponent({
        name: 'system-resource',
        components:{
        },
        setup() {
            const resources = ref();
            const pagination = ref({});
            const resourceStr=ref("");
            let tree={};
            const list = () => {
                Loading.show();
                axios.get("/system/admin/resource/load-tree").then((response) => {
                    Loading.hide();
                    const data=response.data;
                    if (data.success){
                        resources.value = data.content;
                        initTree();
                    }else {
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };



            /**
             * 删除
             */
            const handledelete = (id) => {
                Confirm.show("删除资源后不可恢复，确认删除？", function () {
                    Loading.show();
                    axios.delete("/system/admin/resource/delete/" + id).then((response) => {
                        Loading.hide();
                        const data = response.data;
                        if (data.success) {
                            list();
                            Toast.success("删除成功!");
                        }else{
                            Loading.hide();
                            Toast.warning(data.message);
                        }
                    })
                });
            };

            const save=()=>{
                let _this = this;

                // 保存校验
                if (Tool.isEmpty(resourceStr.value)) {
                    Toast.warning("资源不能为空！");
                    return;
                }
                let json = JSON.parse(resourceStr.value);

                Loading.show();
                axios.post('/system/admin/resource/save', json).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    if (resp.success) {
                        list();
                        Toast.success("保存成功！");
                    } else {
                        Toast.warning(resp.message)
                    }
                })
            };

            /**
             * 初始资源树
             */
           const initTree=()=>{
                let setting = {
                    data: {
                        simpleData: {
                            idKey: "id",
                            pIdKey: "parent",
                            rootPId: "",
                            enable: true
                        }
                    }
                };
                let zNodes=resources.value
                tree = $.fn.zTree.init($("#tree"),setting,zNodes);
                console.log(tree);
                tree.expandAll(true);
            }

            onMounted(() => {
                list();
            });

            return {
                list,
                handledelete,
                pagination,
                save,
                resources,
                resourceStr,
                tree
            }


        }
    });
</script>